<?php
/*------------------------------------------------------------------------
# com_vportfolio - Vportfolio component
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * @package		Joomla
 * @subpackage	Vportfolio
 */
class PortfolioModelCategory extends JModel
{
	/**
	 * Builds the query to select Portfolio categories
	 * @param array
	 * @return string
	 * @access protected
	 */
	function _getCatgoriesQuery( &$options )
	{
		// TODO: Cache on the fingerprint of the arguments
		$db			=& JFactory::getDBO();

		$wheres[] 	= 'a.public = 1';

		$orderBy	= 'a.id';

		/*
		 * Query to retrieve all categories that belong under the Portfolio
		 * section and that are published.
		 */
		$query = 'SELECT a.*'.
				' FROM #__vportfolio_category AS a'.
				' WHERE ' . implode( ' AND ', $wheres ) .
				' ORDER BY ' . $orderBy;

		return $query;
	}
	/**
	 * Builds the query to select Portfolio items
	 * @param array
	 * @return string
	 * @access protected
	 *Modify
	 */
	function _getPortfoliosQuery( &$options )
	{
		// TODO: Cache on the fingerprint of the arguments
		$db			=& JFactory::getDBO();
	
		$catId		= @$options['category_id'];
		$groupBy	= @$options['group by'];
		$orderBy	= @$options['order by'];

		$select = 'i.*,c.name,c.description categoryDescription ';
		$from	= '#__vportfolio AS i';

		$joins[] = 'INNER JOIN #__vportfolio_category AS c on i.categoryId = c.id';
		if (is_array($catId))
		{
			$wheres[] = 'i.categoryId IN ('.implode( ',', $catId ).')';
		}
		elseif($catId)
		{
			$wheres[] = 'i.categoryId = '.$catId;
		}
		
		$wheres[] = 'i.public = 1';
		$wheres[] = 'c.public = 1';

		/*
		 * Query to retrieve all categories that belong under the contacts
		 * section and that are published.
		 */
		$query = 'SELECT ' . $select .
				' FROM ' . $from .
				' ' . implode ( ' ', $joins ) .
				' WHERE ' . implode( ' AND ', $wheres ) .
				($groupBy ? ' GROUP BY ' . $groupBy : '').
				($orderBy ? ' ORDER BY ' . $orderBy : '');

		return $query;
	}
	/**
	 * Gets a list of categories
	 * @param array
	 * @return array
	 */
	function getCategories( $options=array() )
	{
		$query	= $this->_getCatgoriesQuery( $options );
		return $this->_getList( $query, @$options['limitstart'], @$options['limit'] );
	}

	/**
	 * Gets the count of the categories for the given options
	 * @param array
	 * @return int
	 */
	function getCategoryCount( $options=array() )
	{
		$query	= $this->_getCatgoriesQuery( $options );
		return $this->_getListCount( $query );
	}

	/**
	 * Gets a list of categories
	 * @param array
	 * @return array
	 */
	function getPortfolios( $options=array() )
	{
		$query	= $this->_getPortfoliosQuery( $options );
		return $this->_getList( $query, @$options['limitstart'], @$options['limit'] );
	}

	/**
	 * Gets the count of the Portfolio for the given options
	 * @param array
	 * @return int
	 */
	function getPortfolioCount( $options=array() )
	{
		$query	= $this->_getPortfoliosQuery( $options );
		return $this->_getListCount( $query );
	}
	function getDefaultImage( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_image WHERE itemId="'.$id.'" ORDER BY `default` DESC, id ASC ';
		$db->setQuery($query);
		$images = $db->loadObjectList();
		if(count($images) > 0)
			return $images[0];
		else
			return 0;
	}
	//
	function getImages( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_image WHERE itemId="'.$id.'" ORDER BY `default` DESC, id ASC ';
		$db->setQuery($query);
		$images = $db->loadObjectList();
		if(count($images) > 0)
			return $images;
		else
			return 0;
	}
}